<?php


namespace app\crm\model;

use app\admin\model\SysModel;
use app\mini\model\CustomerPointModel;
use think\facade\Db;
use app\admin\model\Common;

class ShopPersonSign extends Common
{

    /**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如CRM模块用crm作为数据表前缀
     */
    protected $name = 'shop_person_sign';


    public function getDataList(array $request): array
    {
        try {
            $request['page']  = isset($request['page']) && intval($request['page']) ? intval($request['page']) : 1;
            $request['limit'] = isset($request['limit']) && intval($request['limit']) ? intval($request['limit']) : 15;

            $person_id       = isset($request['person_id']) && trim($request['person_id']) ? trim($request['person_id']) : '';

            $where      = [];
            array_push($where, ['is_del', '=', 0]);
            if(!empty($person_id)){
                array_push($where, ['person_id', '=', $person_id]);
            }else{
                $data              = [];
                $data['list']      = $list ?? [];
                $data['dataCount'] = $dataCount ?? 0;
                return $data;
            }
            $dataCount = $this->where($where)->count();

            if ($dataCount >0 ) {
                $field = ['id', 'person_id', 'sign_id', 'is_default','create_time', 'sign_img'];
                $list  = $this->where($where)->field($field)->order('is_default','desc')
                    ->limit(($request['page'] - 1) * $request['limit'], $request['limit'])
                    ->select()->toArray();
                foreach ($list as $key => &$val) {
                    $val['is_default_text'] = $val['is_default'] ==1?'默认':'---';
                }
            }
            $data              = [];
            $data['list']      = $list ?? [];
            $data['dataCount'] = $dataCount ?? 0;
            return $data;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return [];
        }
    }

    //删除
    public function personSignDel(array $request){
        $person_id = $request['person_id']??0;
        if(empty($person_id)){
            throw new \Exception('护理师id不合法');
        }
        $sign_id = $request['sign_id']??0;
        if(empty($sign_id)){
            throw new \Exception('签名id不合法');
        }
        $personSign = $this->where('person_id',$person_id)->where('sign_id',$sign_id)->find();
        $personSign->is_del = 1;
        $personSign->update_time = time();
        $res = $personSign->save();
        if($res){
            return['error'=>'删除成功','data'=>[]];
        }else{
            throw new \Exception('签名删除失败');
        }
    }

    //默认签名
    public function personSignDefault(array $request){
        Db::startTrans();
        try{
            $person_id = $request['person_id']??0;
            if(empty($person_id)){
                throw new \Exception('护理师id不合法');
            }
            $sign_id = $request['sign_id']??0;
            if(empty($sign_id)){
                throw new \Exception('签名id不合法');
            }
            $personSign = $this->where('person_id',$person_id)->where('sign_id',$sign_id)->find();
            $personSign->is_default = 1;
            $personSign->update_time = time();
            $res = $personSign->save();

            $personRes = $this->where('person_id',$person_id)->where('sign_id','<>',$sign_id)->update(['is_default'=>0,'update_time'=>time()]);

            if($res && $personRes){
                Db::commit();
                return['error'=>'默认成功','data'=>[]];
            }else{
                throw new \Exception('默认失败');
            }
        }catch (\Exception $e){
            Db::rollback();
            throw new \Exception($e->getMessage());
        }

    }







}
